<script setup lang="ts">
import router from '@/router'
import useLoginStore from '@/store/login/login'
import { defaultMenu } from '@/utils/mapmenu'
import { storeToRefs } from 'pinia'
import { computed } from 'vue'
import { useRoute } from 'vue-router'

defineProps<{
  isFold: boolean
}>()

// 1.调用loginStore获取菜单数据
const loginStore = useLoginStore()
const { userMenus } = storeToRefs(loginStore)

// 2.绑定动态菜单
const defaultsubmenu = computed(() => {
  const route = useRoute()
  const defaultValue = defaultMenu(route.path, userMenus.value)
  return defaultValue.id + ''
})

// 3.点击切换路由
const handleToggleRoute = (subitem: any) => {
  router.push(subitem.url)
}
</script>

<template>
  <div class="main-aside">
    <div class="logo">
      <img class="img" src="@/assets/img/logo.png" alt="logo" />
      <span v-show="!isFold" class="title">Sokach后台管理系统</span>
    </div>
    <el-menu
      :collapse="isFold"
      :default-active="defaultsubmenu"
      text-color="#b7bdc3"
      active-text-color="#fff"
      background-color="#001529"
    >
      <template v-for="item in userMenus" :key="item.id">
        <el-sub-menu :index="item.id + ''">
          <template #title>
            <el-icon>
              <component :is="item.icon.split('el-icon-')[1]" />
            </el-icon>
            <span>{{ item.name }}</span>
          </template>

          <template v-for="subitem in item.children" :key="subitem.id">
            <el-menu-item @click="handleToggleRoute(subitem)" :index="subitem.id + ''">{{
              subitem.name
            }}</el-menu-item>
          </template>
        </el-sub-menu>
      </template>
    </el-menu>
  </div>
</template>

<style lang="less" scoped>
.main-aside {
  width: 100%;
  height: 100%;
  .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    overflow: hidden;

    .img {
      height: 100%;
      margin: 0 10px;
    }

    .title {
      font-size: 15px;
      font-weight: 700;
      color: white;
      white-space: nowrap;
    }
  }
}
.el-menu {
  border-right: none;
  user-select: none;
}

.el-sub-menu {
  .el-menu-item {
    padding-left: 50px !important;
    background-color: #0c2135;
  }

  .el-menu-item:hover {
    color: #fff;
  }

  .el-menu-item.is-active {
    background-color: #0a60bd;
  }
}
</style>
